#include "VertexArray.h"

#include <glad/glad.h>

VertexArray::VertexArray()
{
    glGenVertexArrays(1, &m_rendererId);
    glBindVertexArray(m_rendererId);
}

VertexArray::~VertexArray()
{
    glDeleteVertexArrays(1, &m_rendererId);
}

void VertexArray::bind() const
{
    glBindVertexArray(m_rendererId);
}

void VertexArray::unbind() const
{
    glBindVertexArray(0);
}

void VertexArray::addBuffer(const VertexBuffer &vb, const VertexBufferLayout &layout)
{
    bind();
    vb.bind();

    const auto &attributes = layout.getAttributes();
    for (int i = 0; i < attributes.size(); i++) {
        const auto &element = attributes[i];
        void       *offset  = reinterpret_cast<void *>(element.offset);
        glVertexAttribPointer(element.location, element.count, element.type, element.normalized, layout.getStride(), offset);
        glEnableVertexAttribArray(element.location);
    }
}